package com.base.myproject.client.busi.service.analysis;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.AggregationRowConfig;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 会员充值信息查询，可根据日期，店名查询当天会员充值的情况
 * @author zgw
 *2009-11-30 下午09:12:47
 * javazgw@gmail.com
 */
public class CcodeFinanceAnalysis extends ContentPanel {

	BaseStore<JDBCModelData> store;
	BaseStore<JDBCModelData> storechild;
	
	 TextField<String> name = new TextField<String>();
	 TextField<String> s_cardno= new TextField<String>();
	 //TextField<String> edate = new TextField<String>();
	 DateField qdate  = new DateField();
	//final DatePicker picker = new DatePicker();
	DateTimeFormat activeFormat = DateTimeFormat
	.getFormat("yyyy-M-d");
	BaseComboBoxForm<BaseModelData> systemid_t;
	public CcodeFinanceAnalysis (){
		init();
	}
	
	void init() {	
		setWidth("760px");
		setHeading("会员充值信息查询");
		//initstore();
		initstore();
		initWidget();
	}
	
	
	void initstore()
	{
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");


		type.addField("finance");
		type.addField("finance_real");
		type.addField("finance_gif");
		type.addField("ccode");
		type.addField("updatedate");
		type.addField("dc");
		type.addField("bcode");
		type.addField("bname");
		type.addField("name");
		type.addField("cname");
		type.addField("s_cardno");
		type.getField("finance").setType(Double.class);
		type.getField("finance_real").setType(Double.class);
		type.getField("finance_gif").setType(Double.class);

		StoreInfo info = new StoreInfo();


		info.setTablename("aa");
		info.setNeedAllRow("no");
		info.setLinkparent_chile_columns(new String[]{"ccode"});
		info.setLinkparentcolumns(new String[]{"ccode"});
		info.setParent(store);
	
		String  sql = "";
 
		 info.setSql(sql);
		
		 storechild = new BaseStore<JDBCModelData>(type, info,false);
	}
	void initWidget() {
		
		VerticalPanel vpanel = new VerticalPanel();
		
		vpanel.add(new Html("<br>"));
		vpanel.add(initQuery());
		vpanel.add(new Html("<br>"));
		//vpanel.add(CreateGrid());
		vpanel.add(CreateGrid());
		add(vpanel);
		
	}
	Widget CreateGrid()
	{
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
	//	columns.add(new ColumnConfig("name", "店名", 80));
		columns.add(new ColumnConfig("bname", "充值人", 80));
		columns.add(new ColumnConfig("cname", "会员", 80));
		columns.add(new ColumnConfig("s_cardno", "会员", 80));
//		columns.add(new ColumnConfig("bcode", "会员", 80));
//		columns.add(new ColumnConfig("bcode", "会员", 80));
		columns.add(new ColumnConfig("finance_real", "充值金额", 100));
		columns.add(new ColumnConfig("finance_gif", "赠送金额", 100));
		columns.add(new ColumnConfig("finance", "小计金额", 100));
		//columns.add(new ColumnConfig("email", "会员邮箱", 50));
		columns.add(new ColumnConfig("updatedate", "发生日期", 100));
	//	columns.add(new ColumnConfig("dc", "类型", 50));
	
		AggregationRowConfig averages = new AggregationRowConfig();
//		averages.setHtml("serviceitem", "合计：");
		
		averages.setHtml("bname", "合计：");
		averages.setSummaryType("finance", SummaryType.SUM);
		averages.setSummaryFormat("finance", NumberFormat.getCurrencyFormat());
		averages.setSummaryType("finance_real", SummaryType.SUM);
		averages.setSummaryFormat("finance_real", NumberFormat.getCurrencyFormat());
		averages.setSummaryType("finance_gif", SummaryType.SUM);
		averages.setSummaryFormat("finance_gif", NumberFormat.getCurrencyFormat());
		
		
		 
		ColumnModel cm = new ColumnModel(columns);
		 cm.addAggregationRow(averages);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		BaseGrid  grid = new BaseGrid(storechild, cm,false,false,null);
		grid.setSelectionModel(cbs);
//		grid.setAutoHeight(true);
//		grid.setAutoWidth(true);
		grid.setSize(760, 400);
		return grid;
	}

	
	Widget initQuery()
	{
			HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(new Label("日期："));
		
		qdate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-M-d")); 

		 systemid_t =  com.base.myproject.client.ui.codenamemap.CodeNameMapFactory.createSysteminfoInput_F();
		 systemid_t.validate(false);
		 systemid_t.setToolTip("空表示查询全部");
		 hpanel.add(qdate);
		hpanel.add(new Label("店名："));
		hpanel.add(systemid_t);
		
		Button query = new Button("查询");
		hpanel.add(query);
		query.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				if(qdate.isValid() && (systemid_t.isValid()||systemid_t.getValue()==null))
				{
				StoreInfo info  = storechild.getStoreinfo();;
				info.setSql(initreloadparam());
				storechild.setStoreinfo(info);
				storechild.reload();
				}
			}
			
		});
		 
		return hpanel;
	}
	String initreloadparam()
	{
		
		String sql ="";//= "select * from s_service_finance where datediff(day,updatedate,'"+activeFormat.format(qdate.getValue())+"')=0";

		sql = " select * from ";
		sql += " ( ";
		sql += " select * from s_service_finance where datediff(day,updatedate,'"
				+ activeFormat.format(qdate.getValue()) + "')=0 ";
		sql += " ) as a ";
		sql += " left join ";
		sql += " ( ";
		sql += " select (select name from systeminfo where systeminfo.id = bcode.systemid)  as name,bname,bcode from bcode ";
		sql += " ) as b ";
		sql += " on a.bcode = b.bcode  ";
		sql += " left join   ";
		sql += " (  ";
		sql += " select cname,ccode,s_cardno from ccode   ";
		sql += " )  ";
		sql += " as c  ";
		sql += " on a.ccode = c.ccode  ";
		if(systemid_t.getValue()!=null)
		{
		sql += " where name = '"+systemid_t.getValue().get("name")+"' ";
		}
		sql = URL.encode(sql);
		return sql;
	}
}
